const Mock = require('mockjs');
const mocks = require('./common');

function handleMock() {
  mocks.forEach(item => {
    Mock.mock(
      new RegExp(ENV_CONFIG.API_URL_PREFIX + item.url + '.*'),
      item.type || 'get',
      request => {
        const body = request.body;
        if (typeof body === 'string' && /^[{|[]}]/.test(body)) {
          request.body = JSON.parse(body);
        }
        return item.response(request);
      }
    );
  });
}

module.exports = {
  mocks,
  handleMock
};
